#include "mainimagescene.h"

MainImageScene::MainImageScene(QObject *parent) :
    QGraphicsScene(parent)
{
    startDrawROIFlag = true;
    mousePressedFlag = false;

    pixmapPtr = NULL;
    ROIRectPtr = NULL;

}
void MainImageScene::setView(QGraphicsView *viewPtr_t)
{
    viewPtr=viewPtr_t;
}

void MainImageScene::updateImage(QImage image)
{
    clearImage();
    originalImage.convertFromImage(image);
    drawImage();
    //update();
}

bool MainImageScene::clearImage()
{
    if (pixmapPtr!=NULL)
    {
        removeItem(pixmapPtr);
        delete pixmapPtr;
        pixmapPtr = NULL;
    }
    return true;
}

bool MainImageScene::drawImage()
{
    if (!originalImage.isNull())
    {
        pixmapPtr = addPixmap(originalImage);
        pixmapPtr->setScale(1.0);
        pixmapPtr->setPos(0-originalImage.width()/2, 0-originalImage.height()/2);
        pixmapPtr->setZValue(10);
    }
    return true;
}

void MainImageScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (startDrawROIFlag)
    {
        if (labeledFlag)
        {
            clearROIRect();
        }
        if (mousePressedFlag == false)
        {
            pressPoint = mouseEvent->scenePos() + QPointF(originalImage.width()/2,originalImage.height()/2);
            if(pressPoint.x() < 0)
            {
                pressPoint.setX(0);
            }

            if(pressPoint.y()<0)
            {
                pressPoint.setY(0);
            }
            mousePressedFlag = true;
        }
    }
    //emit pressed();
    QGraphicsScene::mousePressEvent(mouseEvent);
    //update();
}

void MainImageScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if (startDrawROIFlag)
    {
        if (mousePressedFlag == true)
        {
            mousePressedFlag = false;
            QPointF tempPoint;
            QPointF releasePoint;
            releasePoint = mouseEvent->scenePos()+ QPointF(originalImage.width()/2,originalImage.height()/2);;

            //can not exceed boundary
            if (releasePoint.x() >= originalImage.width())
            {
                releasePoint.setX(originalImage.width()-1);
            }

            if (releasePoint.y()>=originalImage.height())
            {
                releasePoint.setY(originalImage.height()-1);
            }
            //find left-top point
            if (pressPoint.x()>releasePoint.x())
            {
                tempPoint.setX(pressPoint.x());
                pressPoint.setX(releasePoint.x());
                releasePoint.setX(tempPoint.x());
            }

            if (pressPoint.y()>releasePoint.y())
            {
                tempPoint.setY(pressPoint.y());
                pressPoint.setY(releasePoint.y());
                releasePoint.setY(tempPoint.y());
            }
            labeledFlag = true;
            ROIRect.setRect(pressPoint.x(),pressPoint.y(),releasePoint.x()-pressPoint.x(),releasePoint.y()-pressPoint.y());
            drawROIRect();
            startDrawROIFlag = false;
        }
    }
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
    update();
}

bool MainImageScene::drawROIRect()
{
    if (!originalImage.isNull())
    {
        QPen pen;
        pen.setWidth(5);
        pen.setColor(QColor(0,0,255));
        pen.setStyle(Qt::DashLine);
        ROIRectPtr = addRect(ROIRect,pen);
        ROIRectPtr->setPos(0-originalImage.width()/2, 0-originalImage.height()/2);
        ROIRectPtr->setZValue(20);
    }
    return true;
}

bool MainImageScene::clearROIRect()
{
    if (ROIRectPtr!=NULL)
    {
        removeItem(ROIRectPtr);
        delete ROIRectPtr;
        ROIRectPtr = NULL;
    }
    return true;
}

QRectF MainImageScene::getROI()
{
    return ROIRect;
}

bool MainImageScene::isSetROI()
{
    return labeledFlag;
}

bool MainImageScene::resetROI()
{
    clearROIRect();
    startDrawROIFlag = true;
    labeledFlag = false;
    return true;
}
